//
//  LFHomeViewController.m
//  青岛生活圈
//
//  Created by 李立达 on 15/11/21.
//  Copyright © 2015年 李立达. All rights reserved.
//

#import "LFHomeViewController.h"
#import "LFloginViewController.h"
#import "AFNetworking.h"
#import "LFHomeModel.h"
#import "LFLocaltionManger.h"
#import "LFAdsTableViewCell.h"
#import "LFGroupTableViewCell.h"
#import "LFFamousTableViewCell.h"
#import "LFGuessTableViewCell.h"
static NSString *identifier=@"IDCell";
@interface LFHomeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic)LFAdsTableViewCell *header;
@property (strong,nonatomic)LFHomeModel *model;
@end

@implementation LFHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.leftButton.hidden=YES;
    self.view.backgroundColor=[UIColor yellowColor];
    self.titleLabel.text=@"首页";
    
    [self loadData];
    [LFLocaltionManger LocationWithSucess:^(CLLocationCoordinate2D coordinate) {
        
        NSLog(@"%lf",coordinate.latitude);
    } Withfaild:^(NSError *faild) {
    }];
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];
    
    _header=[[NSBundle mainBundle]loadNibNamed:@"LFAdsTableViewCell" owner:nil options:nil].lastObject;
    
    _header.frame=CGRectMake(0, 0, KscreedWdith, (425.0-88.0)/2.0);
    
    _tableView.tableHeaderView=_header;
    
    self.automaticallyAdjustsScrollViewInsets=NO;
}


-(void)loginMethod
{
    LFloginViewController *login=[[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"LFloginViewController"];
    [self.navigationController pushViewController:login animated:YES];
    AppDelegate *app=[UIApplication sharedApplication].delegate;
    [app.tabar showHiddenbottmView:YES];
}


-(void)loadData
{
    NSMutableDictionary *dict=[NSMutableDictionary dictionary];
    
    [dict setValue:@"120.38" forKey:@"lon"];
    [dict setValue:@"36.06" forKey:@"lat"];
    [dict setValue:[LFfuction gettimeInterval] forKey:@"time"];
    NSArray *array=@[@"1001",dict[@"lon"],dict[@"lat"],dict[@"time"],IDFios];
    NSString *sign=[LFfuction signappeningWith:array];
    
    [dict setValue:sign forKey:@"sign"];
    [dict setValue:@"1001" forKey:@"app_id"];
    
    NSLog(@"%@",dict);
//    [LFHttpclient httpGetWithUrl:@"?anu=api/1/index/get_index_info" WithPamter:dict WithSucessBlock:^(NSData * JSON) {
////        NSData *str=JSON;
//        NSLog(@"%@",[[NSString alloc]initWithData:JSON encoding:NSUTF8StringEncoding]);
//    } WitherrorBlock:^(NSError *error) {
//        
//        
//    }];
//
    [LFHttpclient httpGetHomeWithUrl:@"anu=api/1/index/get_index_info" WithPamter:dict WithSucessBlock:^(LFHomeModel *model) {
        
        _header.model=model;
        _model=model;
        [_tableView reloadData];
        
    } WitherrorBlock:^(NSError *error) {
        
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section==0||section==1)
    {
        return 1;
    }
    else
    {
        return self.model.guess.list.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section==0)
    {
       
        LFGroupTableViewCell *groupcell=[[LFGroupTableViewCell alloc]init];
        
        groupcell.modle=self.model;
        return groupcell;
        
    }else if(indexPath.section==1)
    {
        LFFamousTableViewCell *cell=[[LFFamousTableViewCell alloc]init];
        cell.list=self.model.famous;
        return cell;
    }
    else
    {
        static NSString *guessID=@"LFGuessTableViewCell";
        LFGuessTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:guessID];
        if(cell==nil)
        {
            cell=[[LFGuessTableViewCell alloc]init];
        }
        cell.model=self.model.guess.list[indexPath.row];
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section==0)
    {
        return 180;
    }
    if(indexPath.section==1)
    {
        return 200;
    }
    if(indexPath.section==2)
    {
        return 105;
    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, KscreedWdith, (66.0/2.0))];
    [view setBackgroundColor:[UIColor whiteColor]];
    UIView *toplineView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, KscreedWdith, 6)];
    [toplineView setBackgroundColor:[UIColor lightGrayColor]];
    
    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, KtableviewCellHeaderHeight-1, KscreedWdith, 1)];
    [line setBackgroundColor:[UIColor lightGrayColor]];
    
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 6, 100, 30)];
    label.textColor=[UIColor redColor];
    
    [view addSubview:label];
    [view addSubview:line];
    [view addSubview:toplineView];
    if(section==1)
    {
        label.text=@"名店推荐";
    }
    else
        if(section==2)
        {
            label.text=@"猜你喜欢";
        }
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }else
        return KtableviewCellHeaderHeight;
}
@end
